package com.aierkeji.b2c.web.listener;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.aierkeji.b2c.utils.AppConfig;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;


@WebListener
@Component
public class PropertyLoadingListener implements ServletContextListener {

    private final Logger log = LoggerFactory.getLogger(getClass());


    @Override
    public void contextDestroyed(ServletContextEvent arg0) {


    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        try {
            Environment env = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(Environment.class);
            String propertyFileLocation = env.getProperty("properties.file.location");
            String[] arr = propertyFileLocation.split(",");

            for(String a:arr){
            	  Properties pros = new Properties();
            	  Path url = Paths.get(a);
                  pros.load(Files.newBufferedReader(url,Charset.forName("UTF-8")));
                  AppConfig.addProperties(pros);
            }

        } catch (Exception e) {
            log.error("异常", e);
        }
    }

}
                                                              